package com.kaikeba.controller;

import com.kaikeba.bean.*;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.UserService;
import com.kaikeba.service.impl.UserServiceImpl;
import com.kaikeba.util.FormatUtil;
import com.kaikeba.util.JsonUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class UserController {

    private UserService service = new UserServiceImpl();

    @ResponseBody("/user/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){
        ResultData<BootstrpUser> data = new ResultData();
        int offset = Integer.parseInt(req.getParameter("offset"));
        int pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
        //数据
        List<User> all = service.findAll(true, offset, pageNumber);
        List<BootstrpUser> bootstrpUsers = new ArrayList<>();
        for (User user : all) {
            int id = user.getId();
            String userName = user.getUserName();
            String userPhone = user.getUserPhone();
            String identityNumber = user.getIdentityNumber();
            String password ="*****";
            String loginTime = user.getLoginTime()!=null?FormatUtil.format(user.getLoginTime()):"-";
            String resgisterTime = user.getRegisterTime()!=null?FormatUtil.format(user.getRegisterTime()):"-";
            BootstrpUser b = new BootstrpUser(id,userName,userPhone,identityNumber,password,resgisterTime,loginTime);
            bootstrpUsers.add(b);
        }

        data.setRows(bootstrpUsers);
        //总人数
        Map<String, Integer> console = service.console();
        Integer sum = console.get("u_sum");
        data.setTotal(sum);
        String s = JsonUtil.toJson(data);
        return s;
    }


    @ResponseBody("/user/add.do")
    public String add(HttpServletRequest req, HttpServletResponse resp){
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        String identityNumber = req.getParameter("identityNumber");
        String password = req.getParameter("password");
        User user = new User(userName,userPhone,identityNumber,password);
        boolean insert = service.insert(user);
        Message message = new Message();
        if (insert){
             message.setStatus(0);
             message.setResult("添加成功");
        }else {
             message.setStatus(-1);
             message.setResult("添加失败");
        }
        String s = JsonUtil.toJson(message);
        return s;
    }

    /**
     * 通过电话号码查找用户
     * @return
     */
    @ResponseBody("/user/find.do")
    public String find(HttpServletRequest req, HttpServletResponse resp){
        String phone = req.getParameter("phone");
        List<User> userList = service.findByUserPhone(phone);
        Message message = new Message();
        if (!userList.isEmpty()){
            User user = userList.get(0);
            message.setStatus(0);
            message.setResult("查找成功");
            message.setData(user);
        }else {
            message.setStatus(-1);
            message.setResult("抱歉，暂无此电话号码");
        }
        String s = JsonUtil.toJson(message);
        System.out.println(s);
        return s;
    }

    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String identityNumber = req.getParameter("identityNumber");
        String password = req.getParameter("password");
        User user = new User(name,phone,identityNumber,password);
        boolean update = service.update(id, user);
        Message message = new Message();
        if (update){
            message.setStatus(0);
            message.setResult("用户更新成功");
        }else {
            message.setStatus(-1);
            message.setResult("用户更新失败");
        }

        String s = JsonUtil.toJson(message);
        return s;
    }
    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        boolean delete = service.delete(id);
        Message message = new Message();
        if (delete){
            message.setStatus(0);
            message.setResult("删除成功");
        }else {
            message.setStatus(-1);
            message.setResult("删除失败");
        }
        String s = JsonUtil.toJson(message);
        return s;
    }

    @ResponseBody("/user/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        Map<String, Integer> console = service.console();
        return JsonUtil.toJson(console);
    }




}
